import Api, { GET_ORDER_LIST, PAY_ORDER, CANCEL_ORDER, CONFIRM_ORDER} from '../../../utils/api.js';
import { handPic,getUrl } from '../../../utils/util.js';


const App = getApp();
Page({
    data: {
        switch_idx: 0,                                   // 新订单 和 历史老订单切换
        tab_idx: 0,                                     // tab 切换
        tab_top: [                                      // tab 所有分类
            { sort: "全部" },
            { sort: "待付款", num: "12" },
            { sort: "待发货", num: "0" },
            { sort: "待收货", num: "0" },
            { sort: "待评价", num: "0" },
            { sort: "售后", num: "0" },
        ],

        isLoading: true,      // 是否正在加载
        hasMore: false,       // 是否有更多数据
        list: {},             // 所有订单的数据

        hasData: true        // 是否有数据
    },
    // _switch
    loadMore: function () {
        let _this = this;
        const { key, p, hasMore,tab_idx,list} = this.data;

        if (hasMore){
            return;
        }
        Api.fetch(GET_ORDER_LIST, { key: key, type: tab_idx,page:p+1}, resp => {
            resp.items.map((val, key) => {
                val.goods.map((v, k) => {
                    v.goods_img = handPic(v.goods_img, 4);
                })
                list.push(val);
            });

            _this.setData({
                tab_idx: tab_idx,
                list: list,
                isLoading: false,
                hasMore: resp.page == resp.next ? true : false,
                p: resp.page,
                total_amount:resp.total_amount
            })
        })
    },
    // tab 切换
    tab: function (e) {
        const _this = this;
        let hasData=true;

        const idx = e.target.dataset.idx;

        const {key} = this.data;

        _this.setData({
            list:{},
            isLoading:true,
            hasMore:true
        })
        
        Api.fetch(GET_ORDER_LIST, { key: key, type: idx }, resp => {
            resp.items.map((val, key) => {
                val.goods.map((v, k) => {
                    v.goods_img = handPic(v.goods_img, 4);
                })
            });

            resp.items.length == 0 ? hasData = false : null;

            _this.setData({
                tab_idx: idx,
                list: resp.items,
                isLoading: false,
                hasMore: resp.page == resp.next ? true : false,
                p: 1,
                hasData: hasData
            })
        })
    },
    cancelOrder:function(e){
        let {idx} = e.currentTarget.dataset,
            {key,list} = this.data,
            _this = this;
        wx.showModal({
            title: '取消订单',
            content: '确认取消订单吗?',
            success: function (res) {
                if (res.confirm) {
                    Api.fetch(CANCEL_ORDER, { key: key, ordersn: list[idx].order_sn}, resp => {
                        list.splice(idx,1);

                        _this.setData({
                            list:list
                        });

                    })
                }
            }
        })
       
    },

    onLoad: function (options) {
        let idx = options.type?options.type:0,
            _this = this,
            key;
        let hasData = true;
        //判断是否登录
        if (Api.checkLogin(function () {
            _this.onLoad(options);
        })) {
            key = Api.getKey();
            _this.setData({key:key})
        }


        Api.fetch(GET_ORDER_LIST,{key:key,type:idx},resp=>{
            resp.items.map((val,key) => {
                val.goods.map((v,k)=>{
                    v.goods_img = handPic(v.goods_img,4);
                })
            });

            resp.items.length == 0 ? hasData = false : null;
            _this.setData({
                tab_idx:idx,
                list:resp.items,
                isLoading:false,
                hasMore:resp.page == resp.next?true:false,
                p:1,
                hasData: hasData
            })
        })

    },
    navigateTo: function (e) {
        let  url;
        let { tip, idx} = e.currentTarget.dataset;

    
        if (tip =='order_detail'){
            url = getUrl(tip,{order_sn:idx})
        } else if (tip == 'pay'){
            url = '../../payment/payment'
        } else if (tip == 'comment') {
            url = getUrl(tip,{id:idx})
        }else if (tip == 'delivery'){
            url = '/pages/mine/logistics/logistics'
        }

        App.navigateTo(url)
    },
    dopay:function(e){
        
        const {key} = this.data;
        let order_sn = e.currentTarget.dataset.sn;

        wx.showLoading({
            title: '支付中',
        })

        Api.fetch(PAY_ORDER,{key:key,order_sn:order_sn},resp => {
            wx.requestPayment({
                'timeStamp': resp.timeStamp,
                'nonceStr': resp.nonceStr,
                'package': resp.package,
                'signType': 'MD5',
                'paySign': resp.paySign,
                'success': function (res) {
                    wx.hideLoading();
                    wx.redirectTo({
                        url: '/pages/success/success',
                    })
                },
                'fail': function (res) {
                    wx.hideLoading();
                    wx.showToast({
                        title: '支付取消',
                    })
                }
            })
        })
    },
    //确认收货
    confirmOrder:function(e){
        let {key,list}  = this.data,
            {orderid,idx} = e.currentTarget.dataset,
            _this = this;
        wx.showModal({
            title: '我要确认收货',
            success:function(){
                Api.fetch(CONFIRM_ORDER,{key:key,orderid:orderid},resp=>{
                    list.splice(idx,1);
                    _this.setData({
                        list:list
                    })
                })
            }
        })

    },
    /** 返回首页 */
    goIndex:function(){
        wx.switchTab({
            url: '../../index/index'
        })
    },
    onReady: function () {

    },

    onShow: function () {

    },

    onHide: function () {

    },

    onUnload: function () {

    },

    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.loadMore();
    },

    onShareAppMessage: function () {

    }
})